package com.dhq.myapp.util

import android.bluetooth.BluetoothDevice
import android.content.Context
import android.util.Log
import com.rokid.cxr.client.extend.CxrApi
import com.rokid.cxr.client.extend.callbacks.BluetoothStatusCallback
import com.rokid.cxr.client.utils.ValueUtil

/**
 * DESC
 * Created by DHQ on 2025/10/11.
 */
class CXRUtil(val mListener: CXRUtil.ConnectListener) {

    private val TAG = "CXRUtil"
//    private var mListener:ConnectListener? = null;

    /**
     * Init Bluetooth
     *
     * @param context   Application Context
     * @param device     Bluetooth Device
     */
    fun initDevice(context: Context, device: BluetoothDevice){
//        mListener = listener;
        /**
         * Init Bluetooth
         *
         * @param context   Application Context
         * @param device     Bluetooth Device
         * @param callback   Bluetooth Status Callback
         */
        CxrApi.getInstance().initBluetooth(context, device,  object : BluetoothStatusCallback {
            /**
             * Connection Info
             *
             * @param socketUuid   Socket UUID
             * @param macAddress   Classic Bluetooth MAC Address
             * @param rokidAccount Rokid Account
             * @param glassesType  Device Type, 0-no display, 1-have display
             */
            override fun onConnectionInfo(
                socketUuid: String?,
                macAddress: String?,
                rokidAccount: String?,
                glassesType: Int
            ) {
                Log.e("连接成功11","连接成功")
                socketUuid?.let { uuid ->
                    macAddress?.let { address->
                        connect(context, uuid, address)
                    }?:run {
                        mListener?.onFailed()
                        Log.e(TAG, "macAddress is null")
                    }
                }?:run{
                    mListener?.onFailed()
                    Log.e(TAG, "socketUuid is null")
                }
            }

            /**
             * Connected
             */
            override fun onConnected() {
                mListener?.onConnected()
                Log.e("连接成功","连接成功")
            }

            /**
             * Disconnected
             */
            override fun onDisconnected() {
                mListener?.onDisconnected()
                Log.e("连接成功","连接断开")
            }

            /**
             * Failed
             *
             * @param errorCode   Error Code:
             * @see ValueUtil.CxrBluetoothErrorCode
             * @see ValueUtil.CxrBluetoothErrorCode.PARAM_INVALID  Parameter Invalid
             * @see ValueUtil.CxrBluetoothErrorCode.BLE_CONNECT_FAILED BLE Connect Failed
             * @see ValueUtil.CxrBluetoothErrorCode.SOCKET_CONNECT_FAILED Socket Connect Failed
             * @see ValueUtil.CxrBluetoothErrorCode.UNKNOWN Unknown
             */
            override fun onFailed(p0: ValueUtil.CxrBluetoothErrorCode?) {
                mListener?.onFailed()
                Log.e("连接成功","连接失败"+p0?.errorCode)
            }

        })
    }



    /**
     *  Connect
     *
     *  @param context   Application Context
     *  @param socketUuid   Socket UUID
     *  @param macAddress   Classic Bluetooth MAC Address
     */
    fun connect(context: Context, socketUuid: String, macAddress: String){
        /**
         * Connect
         */
        CxrApi.getInstance().connectBluetooth(context, socketUuid, macAddress, object : BluetoothStatusCallback{
            /**
             * Connection Info
             *
             * @param socketUuid   Socket UUID
             * @param macAddress   Classic Bluetooth MAC Address
             * @param rokidAccount Rokid Account
             * @param glassesType  Device Type, 0-no display, 1-have display
             */
            override fun onConnectionInfo(
                socketUuid: String?,
                macAddress: String?,
                rokidAccount: String?,
                glassesType: Int
            ) {
                //
            }

            /**
             * Connected
             */
            override fun onConnected() {
                Log.d(TAG, "Connected1")
            }

            /**
             * Disconnected
             */
            override fun onDisconnected() {
                Log.d(TAG, "Disconnected2")
            }

            /**
             * Failed
             *
             * @param errorCode   Error Code:
             * @see ValueUtil.CxrBluetoothErrorCode
             * @see ValueUtil.CxrBluetoothErrorCode.PARAM_INVALID  Parameter Invalid
             * @see ValueUtil.CxrBluetoothErrorCode.BLE_CONNECT_FAILED BLE Connect Failed
             * @see ValueUtil.CxrBluetoothErrorCode.SOCKET_CONNECT_FAILED Socket Connect Failed
             * @see ValueUtil.CxrBluetoothErrorCode.UNKNOWN Unknown
             */
            override fun onFailed(p0: ValueUtil.CxrBluetoothErrorCode?) {
                Log.e(TAG, "Failed3")
            }

        })
    }

    /**
     * Get Connection Status
     *
     * @return  Connection Status: true-connected, false-disconnected
     */
    fun getConnectionStatus(): Boolean{
        return CxrApi.getInstance().isBluetoothConnected
    }

    /**
     * DeInit Bluetooth
     */
    fun deInit(){
        CxrApi.getInstance().deinitBluetooth()
    }

    interface ConnectListener{
        fun onConnected()
        fun onDisconnected()
        fun onFailed()
    }

}